package com.kukudb.asaproject.controller.api;


import com.kukudb.asaproject.pojo.CardKeys;
import com.kukudb.asaproject.pojo.LicenseUsageLogs;
import com.kukudb.asaproject.pojo.ResponseMessage;
import com.kukudb.asaproject.pojo.User;
import com.kukudb.asaproject.pojo.paramsDto.CreateCardKeysDto;
import com.kukudb.asaproject.pojo.paramsDto.UserCardKeyDto;
import com.kukudb.asaproject.pojo.resultDto.UseCardKeyDto;
import com.kukudb.asaproject.serveice.CardKeysImpl;
import com.kukudb.asaproject.serveice.LicenseUsageLogsImpl;
import jakarta.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController     //接口方法返回对象  转换成json数据
@RequestMapping("/api/server")    // localhost:81/api/user
public class CardKeysApi {
    @Autowired
    CardKeysImpl cardKeysImpl ;

    @Autowired
    LicenseUsageLogsImpl licenseUsageLogsImpl ;
    Logger logger = LoggerFactory.getLogger(CardKeysApi.class);
    @PostMapping("/createCardKeys")
    public ResponseMessage<List<CardKeys>> getCardKeys(@RequestBody CreateCardKeysDto createCardKeysDto , HttpSession session) {
        logger.info("createCardKeysDto: " + createCardKeysDto.toString());
        // 从session中获取用户id
         User user = (User) session.getAttribute("user");
         if(user == null){
             return new ResponseMessage(400, "error", "请先登录");
         }
         logger.info("userId: " + user.getUserId());
         if("2".equals(user.getRole())){
             return new ResponseMessage(400, "error", "权限不足");
         }

        createCardKeysDto.setCreatorId(user.getUserId());
        List<CardKeys> cardKeys = cardKeysImpl.createCardKeys(createCardKeysDto);
        return new ResponseMessage(200, "success", cardKeys);
    }

    /***
     * 获取用户卡密列表
     * @param session
     * @return
     */
    @GetMapping("/getCardKeys")
    public ResponseMessage<List<CardKeys>> getCardKeys(HttpSession session) {
        User user = (User) session.getAttribute("user");
        logger.info("userId: " + user.getUserId());
        if("2".equals(user.getRole())){
            return new ResponseMessage(400, "error", "权限不足");
        }
        List<CardKeys> cardKeys = cardKeysImpl.getCardKeys(user.getUserId());
        return new ResponseMessage(200, "success", cardKeys);
    }
    //使用卡密
    @PostMapping("/useCardKeys")
    public ResponseMessage<UseCardKeyDto> useCardKeys(@RequestBody UserCardKeyDto userCardKeyDto, HttpSession session) {
        logger.info("userCardKeyDto: " + userCardKeyDto.toString());
        User user = (User) session.getAttribute("user");
        logger.info("userId: " + user.getUserId());
        UseCardKeyDto cardKeys = cardKeysImpl.useCardKeys(userCardKeyDto, user.getUserId());

        LicenseUsageLogs licenseUsageLogs = new LicenseUsageLogs();
        licenseUsageLogs.setUserId(user.getUserId());
        licenseUsageLogs.setLicenseId(cardKeys.getLicenseId());
        licenseUsageLogs.setOperation("使用卡密");
        licenseUsageLogs.setUsageTime(new Date());
        licenseUsageLogs.setIpAddress((String) session.getAttribute("IP"));
        licenseUsageLogs.setUsedValue(cardKeys.getUsedValue());
        licenseUsageLogsImpl.addLicenseUsageLogs(licenseUsageLogs);
        return new ResponseMessage(200, "success", cardKeys);
    }

    //删除卡密,删除登录用户创建的所有卡密
    @GetMapping("/deleteCardKeys")
    public ResponseMessage<String> deleteCardKeys(HttpSession session) {
        User user = (User) session.getAttribute("user");
        logger.info("userId: " + user.getUserId());
        if("2".equals(user.getRole())){
            return new ResponseMessage(400, "error", "权限不足");
        }
        cardKeysImpl.deleteCardKeys(user.getUserId());
        return new ResponseMessage(200, "success", "删除成功");
    }
    //删除所有卡密
    @GetMapping("/deleteAllCardKeys")
    public ResponseMessage<String> deleteAllCardKeys(HttpSession session) {
        User user = (User) session.getAttribute("user");
        logger.info("userId: " + user.getUserId());
        if(!"0".equals(user.getRole())){
            return new ResponseMessage(400, "error", "权限不足");
        }
        cardKeysImpl.deleteAllCardKeys();
        return new ResponseMessage(200, "success", "删除成功");
    }

}
